let config = require('../../../utils/config.js');
let util = require('../../../utils/util.js');
let demandTools = require('../../../utils/demand.js');
let API_URL = config.API_URL;
let app = getApp();
let page, demandId;
// 推荐
let getR = that => {
    let params = {
        demand_id: demandId,
        page: page
    }
    util.PRL(API_URL.recommends_list, params)
        .then(res => {
            console.log(res)
            let list = res.data.data.list;
            let recommends;
            if (page == 1) {
                recommends = [];
            } else {
                recommends = that.data.recommends;
            }
            recommends = recommends.concat(list);

            console.log(recommends)
            that.setData({
                recommends: recommends
            })
            if (list.length != 0) {
                page++
            }
        }).catch(res => {
            console.log(res);
        })
}
// 邀请
let getI = that => {
    let params = {
        demand_id: demandId,
        page: page
    }
    util.PRL(API_URL.invites_list, params)
        .then(res => {
            let list = res.data.data.invites;
            let invites;
            if (page == 1) {
                invites = [];
            } else {
                invites = that.data.invites;
            }
            invites = invites.concat(list);
            that.setData({
                invites: invites
            })
            if (list.length != 0) {
                page++
            }
        }).catch(res => {
            console.log(res);
        })
}
Page({
    data: {
        tag: 'recommends',
        template: "recommends",
        owner: false
    },
    onLoad: function (options) {
        let that = this;
        demandId = app.globalData.demand.demandId;
        console.log(app.globalData.demand)
        // 如果登录了 则拿到缓存的值 用于判断是否登录 是否是自己
        let user = my.getStorageSync({ key: 'user' }).data;

        let owner = app.globalData.demand.demandUserId == user.userId ? true : false;
        console.log(owner)
        page = 1;
        let isTGX = my.getStorageSync({ key: 'isTGX' }).data;
        console.log(isTGX)
        let demand = app.globalData.demand;
        that.setData({
            isTGX: isTGX,
            demand: demand,
            owner: owner
        })
        getR(that)
    },
    onShow: function () {
        let that = this;
        if (app.globalData.refresh) {
            page = 1;
            app.globalData.refresh = false;
            if (that.data.tag == 'invites') {
                getI(that)
            } else {
                getR(that)
            }
        }
        let user = my.getStorageSync({ key: 'user' }).data;

        let isLogin;
        if (user) {
            isLogin = true;
        } else {
            isLogin = false;
        }
        console.log(isLogin)
        that.setData({
            isLogin: isLogin
        })
    },
    // 标签切换
    tag: function (e) {
        let that = this;
        let tag = e.currentTarget.dataset.tag;
        console.log(tag)
        if (tag == that.data.tag) {
            return
        }
        that.setData({
            tag: tag,
            template: tag
        })
        page = 1;
        if (tag == 'invites') {
            getI(that)
        } else {
            getR(that)
        }
    },
    // 预览图片
    previewR: function (e) {
        console.log(e)
        let recommends = this.data.recommends;
        console.log(recommends)
        let index = e.currentTarget.dataset.index;

        let images = recommends[index].images;

        let image = e.currentTarget.dataset.url;

        my.previewImage({
            current: image,
            urls: images
        })
    },
    // 成交
    order: function (e) {
        let index = e.currentTarget.dataset.index;
        let goods = {};
        // 获取到当前的商品
        let r = this.data.recommends[index];
        // 赋值给全局变量goods
        goods.img = r.images[0];
        goods.name = r.description;
        goods.price = r.price;
        app.globalData.good = goods;
        my.navigateTo({
            url: '/pages/demand/order/order?recommendId=' + r.recommendId,
        })
    },
    // 关闭求购
    close: function (e) {
        let that = this;
        let demand = that.data.demand;
        console.log(demand)
        let params = {
            demand_id: demandId
        }
        util.PRL(API_URL.demand_del, params)
            .then(res => {
                app.globalData.refresh = true;
                my.navigateBack({
                    delta: 1
                })
            }).catch(res => {
                console.log(res);
            })
    },
    // 重新发布
    open: function (e) {
        my.navigateTo({
            url: '/pages/demand/add/add?action=edit',
        })
    },
    // 推荐
    addR: function (e) {
        demandTools.addR(demandId);
    },
    // 邀请
    addI: function (e) {
        demandTools.addI(demandId);
    },
})